package com.yxkj.syh.app.huarong.activities.user;


import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.BR;
import com.syh.app.basic.base.BaseFragment;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.AppointmentListAdp;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.FragmentUserBinding;

/**
 * 用户 个人
 */
public class UserFragment extends BaseFragment<FragmentUserBinding, UserVM> {

    private AppointmentListAdp mAppointmentListAdp;

    public UserFragment() {
        // Required empty public constructor
    }

    public static UserFragment newInstance() {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.userVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_user;
    }

    @Override
    protected void initView() {
        mAppointmentListAdp = new AppointmentListAdp();
        mVDBinding.rvAppointments.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mAppointmentListAdp.bindToRecycleView(mVDBinding.rvAppointments);
        mAppointmentListAdp.setOnViewClick(car -> {
            ARouter.getInstance().build(ArouterPath.APPOINTMENT_ACTIVITY).withLong("id", car.getId()).navigation();
        });

        mVDBinding.srlAppointments.setOnLoadMoreListener(refreshLayout -> {
            mAppointmentListAdp.clear();
            mViewModel.appointments();
        });
        mVDBinding.srlAppointments.setEnableLoadMore(false);


    }

    @Override
    protected void initData() {
        mViewModel.userInfo();
        mViewModel.appointments();
        mViewModel.report();
    }

    @Override
    protected void visibleAgain() {
       initData();
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldAppointmentsResponse.observe(this, carResponse -> {
            if (mVDBinding.srlAppointments.isRefreshing()) {
                mVDBinding.srlAppointments.finishRefresh();
            }
            if (carResponse != null) {
                mAppointmentListAdp.setNewData(carResponse.getData().getRecords());
            } else {
                mAppointmentListAdp.notifyDataSetChanged();
            }
        });
    }


}
